#pragma once

#include <system_error>
#include "helper/resource/IOpenable.h"

namespace helper {
namespace resource {

template<>
class Reopenable<std::error_code> : public IOpenable<std::error_code>{
public:
	typedef std::error_code return_type;
	~Reopenable() override {}

	return_type open() override {
		if(!isOpened()){
			auto && ec = _open();
			if(!ec){
				_opened = true;
			}
			return ec;
		}
		return return_type();
	}

	return_type close() override {
		if(isOpened()){
			auto && ec = _close();
			if(!ec){
					_opened = false;
			}
			return ec;
		}
		return return_type();
	}

	bool isOpened() const override {
		return _opened;
	}

protected:
	virtual return_type _open() = 0;
	virtual return_type _close() = 0;
private:
	bool _opened = false;
};

}//namespace resource
}//namespace helper
